package com.hankol.newretail

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.BaseConstant
import com.baselibrary.Constants
import com.baselibrary.extension.checkNotNull
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.*
import com.baselibrary.activity.BaseActivity
import com.hankol.newretail.bean.EvaluationBean
import com.hankol.newretail.databinding.ActivityEvaluationDetailBinding
import com.hankol.newretail.viewmodel.OrderAboutViewModel

/**
 *@author:hanke
 *2022/9/5
 *desc:
 */
@Route(path = ActivityRouter.NEW_RETAIL.EVALUATION_DETAIL)
class EvaluationDetailActivity : BaseActivity(), View.OnClickListener {
    private lateinit var dataBinding: ActivityEvaluationDetailBinding
    private lateinit var viewModel: OrderAboutViewModel
    private lateinit var data: EvaluationBean

    override fun getLayoutId(): Int {
        initImmersion(true)
        return R.layout.activity_evaluation_detail
    }

    override fun init() {
        intent.extras?.let {
            data = it.getParcelable<EvaluationBean>("evaluation_detail")!!
        }
        setStatusBarHeight(R.color.color_FFFFFF)
        StatusBarUtil.setStatusBarDarkTheme(this, true)
        dataBinding = getDataBinding<ActivityEvaluationDetailBinding>()
        viewModel = getActivityViewModelProvider(this).get(OrderAboutViewModel::class.java)
        dataBinding.ivBack.setOnClickListener { finish() }
        setUI(data)
    }

    private fun setUI(data: EvaluationBean) {
        GlideUtils.loadImg_228(this, data.skuCover, dataBinding.ivGoodsCover)
        dataBinding.tvGoodsName.text = data.spuName
        dataBinding.llGoodsInfo.setOnClickListener(this)
        if (data.user.checkNotNull()) {
            dataBinding.civAvatar.setData(data.user.icon, data.user.isFans == 1)
            dataBinding.tvName.text = data.user.name
            dataBinding.civAvatar.setOnClickListener(this)
            dataBinding.tvName.setOnClickListener(this)
        }
        dataBinding.tvTime.text = TimeUtil.fromToday(data.commentTime)
        dataBinding.erbRatingLevel.setStarStyle(12, true, ScreenUtils.dpToPx(2).toFloat(), 4, 12)
        dataBinding.erbRatingLevel.setRatingLevel(data.commentResult, false)
        dataBinding.tvGoodsScale.text = SpecUtils.getNormsStr(data.specList)
        if (data.commentContent.checkNotNull()) {
            dataBinding.tvContent.visibility = View.VISIBLE
            dataBinding.tvContent.text = data.commentContent
        } else {
            dataBinding.tvContent.visibility = View.GONE
        }

        if (data.mediaInfoList.checkNotNull()) {
            var urlList = ArrayList<String>()
            var typeList = ArrayList<Int>()
            for (media in data.mediaInfoList) {
                typeList.add(media.type)
                if (media.type == 1) {
                    urlList.add(media.videoUrl)
                } else {
                    urlList.add(media.coverUrl)
                }
            }
            dataBinding.photoContentView.setImageUrls(urlList, typeList)
            dataBinding.photoContentView.spacing = ScreenUtils.dpToPx(5).toFloat()
            dataBinding.photoContentView.setCallback { position -> //图片详情
                val bundle = Bundle()
                bundle.putStringArrayList(Constants.KEY_URLS, urlList)
                bundle.putInt(Constants.KEY_INDEX, position)
                bundle.putStringArrayList(Constants.KEY_COMMENT_CONTENT, arrayListOf(data.commentContent))
                bundle.putStringArrayList(Constants.KEY_COMMENT_TITLE, arrayListOf())
                ActivityRouter.startActivity(ActivityRouter.WINE_CELLAR.IMG_VIDEO_DETAIL, bundle)
            }
        } else {
            dataBinding.photoContentView.visibility = View.GONE
        }
        if (data.ownerReplyList.checkNotNull()) {
            dataBinding.llShopReply.visibility = View.VISIBLE
            if (data.ownerReplyList[0].user.checkNotNull()) {

                dataBinding.civShopAvatar.setData(data.ownerReplyList[0].user.icon, data.ownerReplyList[0].user.isFans == 1)
                dataBinding.tvShopName.text = data.ownerReplyList[0].user.name
                dataBinding.tvShopTime.text = TimeUtil.fromToday(data.ownerReplyList[0].replytime)
                dataBinding.tvShopContent.text = data.ownerReplyList[0].content
                dataBinding.civShopAvatar.setOnClickListener(this)
                dataBinding.tvShopName.setOnClickListener(this)
            }

        } else {
            dataBinding.llShopReply.visibility = View.GONE
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.civ_avatar, R.id.tv_name -> {
                goUserHomePage(data.user.id)
            }
            R.id.civ_shop_avatar, R.id.tv_shop_name -> {
                goUserHomePage(data.ownerReplyList[0].user.id)
            }
            R.id.ll_goods_info -> {
                val map = HashMap<String, Any>()
                map[Constants.GOODS_DETAIL_ID] = data.spuId
                map[Constants.GOODS_SKU_ID] = data.skuId
                map[Constants.GOODS_DETAIL_SOURCE] = "评价详情页"
                ARouterUtils.jump(ActivityRouter.NEW_RETAIL.GOODS_DETAUILS, map)
            }
        }
    }

    private fun goUserHomePage(userId: Int) {
        val hashMap = HashMap<String, Any>()
        hashMap[BaseConstant.USER_ID] = userId
        ARouterUtils.jump(ActivityRouter.MINE_CENTER.USER_INFO, hashMap)
    }

}